package org.jeecg.modules.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.crm.entity.Receipt;
import org.jeecg.modules.crm.entity.ReceiptReceiptDetail;
import org.jeecg.modules.crm.mapper.ReceiptReceiptDetailMapper;
import org.jeecg.modules.crm.mapper.ReceiptMapper;
import org.jeecg.modules.crm.service.IReceiptService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.time.Year;
import java.util.List;
import java.util.Collection;

/**
 * @Description: Receipt
 * @Author: jeecg-boot
 * @Date:   2025-04-22
 * @Version: V1.0
 */
@Service
public class ReceiptServiceImpl extends ServiceImpl<ReceiptMapper, Receipt> implements IReceiptService {

	@Autowired
	private ReceiptMapper receiptMapper;
	@Autowired
	private ReceiptReceiptDetailMapper receiptReceiptDetailMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		receiptReceiptDetailMapper.deleteByMainId(id);
		receiptMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			receiptReceiptDetailMapper.deleteByMainId(id.toString());
			receiptMapper.deleteById(id);
		}
	}

	@Override
	public String getSerialNumber(String receiptType) {
		int currentYear = Year.now().getValue();
		Integer serialNumber = receiptMapper.getSerialNumber(receiptType);
		if (serialNumber==0){
			serialNumber=1;
		}else {
			serialNumber = serialNumber+1;
		}
		String formattedSequence = String.format("%04d", serialNumber);
		return String.valueOf(currentYear) + formattedSequence;
	}

}
